﻿using Nop.Admin.Models.XJ;
using Nop.Core;
using Nop.Core.Domain.XJ;
using Nop.Services.Common;
using Nop.Services.Customers;
using Nop.Services.Directory;
using Nop.Services.Logging;
using Nop.Services.Media;
using Nop.Services.Security;
using Nop.Services.XJ;
using Nop.Web.Framework.Controllers;
using Nop.Web.Framework.Kendoui;
using System;
using System.Linq;
using System.Web.Mvc;

namespace Nop.Admin.Controllers
{
	public class EquipmentStockLogController : BaseAdminController
	{
		#region Fields
		private readonly IPermissionService _permissionService;
		private readonly IPictureService _pictureService;
		private readonly IWorkContext _workContext;
		private readonly IOrganizationService _organizationServce;
		private readonly ICustomerService _customerService;
		private readonly IGenericAttributeService _genericAttributeService;
		private readonly IXJPlaceService _xjPlaceService;
		private readonly IXJEquipmentCategoryService _xJEquipmentCategoryService;
		private readonly IXJEquipmentService _xJEquipmentService;
		private readonly IXJEquipmentStockService _xJEquipmentStockService;
		private readonly IXJEquipmentStockLogService _xJEquipmentStockLogService;
		private readonly IXJEquipmentStockInService _xJEquipmentStockInService;
		private readonly IXJEquipmentStockInDetailService _xJEquipmentStockInDetailService;
		private readonly IXJEquipmentStockOutService _xJEquipmentStockOutService;
		private readonly IXJEquipmentStockOutDetailService _xJEquipmentStockOutDetailService;
		private readonly IXJInspectionService _xJInspectionService;
		private readonly IXJEquipmentStockInService _xJInspectionPlanService;
		private readonly IXJMaintenanceService _xJMaintenanceService;
		private readonly IXJMaintenanceCommentService _xJMaintenanceCommentService;
		private readonly ICustomerActivityService _customerActivityService;
		private readonly ILogger _logger;
		#endregion

		#region Constructors
		public EquipmentStockLogController(IPermissionService permissionService,
			IPictureService pictureService,
			IWorkContext workContext,
			IOrganizationService organizationServce,
			ICustomerService customerService,
			IGenericAttributeService genericAttributeService,
			IXJPlaceService xjPlaceService,
			IXJEquipmentCategoryService xJEquipmentCategoryService,
			IXJEquipmentService xJEquipmentService,
			IXJEquipmentStockService xJEquipmentStockService,
			IXJEquipmentStockLogService xJEquipmentStockLogService,
			IXJEquipmentStockInService xJEquipmentStockInService,
			IXJEquipmentStockInDetailService xJEquipmentStockInDetailService,
			IXJEquipmentStockOutService xJEquipmentStockOutService,
			IXJEquipmentStockOutDetailService xJEquipmentStockOutDetailService,
			IXJInspectionService xJInspectionService,
			IXJEquipmentStockInService xJInspectionPlanService,
			IXJMaintenanceService xJMaintenanceService,
			IXJMaintenanceCommentService xJMaintenanceCommentService,
			ICustomerActivityService customerActivityService,
			ILogger logger)
		{
			this._permissionService = permissionService;
			this._pictureService = pictureService;
			this._workContext = workContext;
			this._organizationServce = organizationServce;
			this._customerService = customerService;
			this._genericAttributeService = genericAttributeService;
			this._xjPlaceService = xjPlaceService;
			this._xJEquipmentCategoryService = xJEquipmentCategoryService;
			this._xJEquipmentService = xJEquipmentService;
			this._xJEquipmentStockService = xJEquipmentStockService;
			this._xJEquipmentStockLogService = xJEquipmentStockLogService;
			this._xJEquipmentStockInService = xJEquipmentStockInService;
			this._xJEquipmentStockInDetailService = xJEquipmentStockInDetailService;
			this._xJEquipmentStockOutService = xJEquipmentStockOutService;
			this._xJEquipmentStockOutDetailService = xJEquipmentStockOutDetailService;
			this._xJInspectionService = xJInspectionService;
			this._xJInspectionPlanService = xJInspectionPlanService;
			this._xJMaintenanceService = xJMaintenanceService;
			this._xJMaintenanceCommentService = xJMaintenanceCommentService;
			this._customerActivityService = customerActivityService;
			this._logger = logger;
		}
		#endregion

		#region List
		public ActionResult Index()
		{
			return RedirectToAction("List");
		}

		public virtual ActionResult List()
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageEquipments))
				return AccessDeniedView();

			var model = new EquipmentStockLogListModel();
			return View(model);
		}

		[HttpPost]
		public virtual ActionResult List(DataSourceRequest command, EquipmentStockLogListModel model)
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageEquipments))
				return AccessDeniedKendoGridJson();

			var ins = _xJEquipmentStockLogService.GetAllStockLogs(
				typeId: model.SearchTypeId,
				stockId: model.SearchStockId,
				equipmentId: model.SearchEquipmentId,
				pageIndex: command.Page - 1,
				pageSize: command.PageSize);
			var gridModel = new DataSourceResult
			{
				ExtraData = _xJEquipmentStockLogService.GetTotal(
					typeId: model.SearchTypeId,
					stockId: model.SearchStockId,
					equipmentId: model.SearchEquipmentId
				),
				Data = ins.Select(x =>
				{
					x.RelatedTypeName = Enum.GetName(typeof(StockRelatedTypeEnum), x.RelatedType);
					return x;
				}),
				Total = ins.TotalCount
			};
			return Json(gridModel);
		}

		public virtual ActionResult LogPopup(int eId)
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageEquipments))
				return AccessDeniedView();

			var model = new EquipmentStockLogListModel
			{
				SearchEquipmentId = eId
			};

			return View(model);
		}
		#endregion

		#region 入库
		public virtual ActionResult InPopup(int eId)
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageEquipments))
				return AccessDeniedView();

			var model = new EquipmentStockLogModel
			{
				RelatedType = (int)StockRelatedTypeEnum.入库
			};
			if (eId > 0)
			{
				var equipment = _xJEquipmentService.GetXJEquipmentById(eId);
				if (null != equipment)
				{
					model.RelatedTypeName = Enum.GetName(typeof(StockRelatedTypeEnum), model.RelatedType);
					model.EquipmentId = equipment.Id;
					model.Name = equipment.Name;
					model.Manufacturer = equipment.Manufacturer;
					model.Specification = equipment.Specification;
					model.Price = equipment.Price;
					model.Amount = 0;
					model.Comment = null;
					model.CreatedOn = DateTime.Now;
				}
			}
			return View(model);
		}

		[HttpPost]
		[FormValueRequired("save")]
		public virtual ActionResult InPopup(string btnId, EquipmentStockLogModel model)
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageEquipments))
				return AccessDeniedView();

			if (ModelState.IsValid)
			{
				var equipment = _xJEquipmentService.GetXJEquipmentById(model.EquipmentId);
				if (null == equipment)
				{
					ErrorNotification("设施设备无效");
					ViewBag.RefreshPage = false;
					ViewBag.btnId = btnId;
					return View(model);
				}
				var stockLog = new XJEquipmentStockLog
				{
					RelatedType = (int)StockRelatedTypeEnum.入库,
					EquipmentId = model.EquipmentId,
					Name = model.Name,
					Manufacturer = model.Manufacturer,
					Specification = model.Specification,
					Price = model.Price,
					Amount = model.Amount,
					Comment = model.Comment,
					CustomerId = _workContext.CurrentCustomer.Id,
					CreatedOn = DateTime.Now,
				};
				_xJEquipmentStockLogService.InsertXJEquipmentStockLog(stockLog);

				equipment.Stock += model.Amount;
				_xJEquipmentService.UpdateXJEquipment(equipment);

				//activity log
				_customerActivityService.InsertActivity("AddNewEquipmentStockLog", "新增入库：{0}", stockLog.Id);
				ViewBag.RefreshPage = true;
				ViewBag.btnId = btnId;
			}
			else
			{
				ViewBag.RefreshPage = false;
				ViewBag.btnId = btnId;
			}

			return View(model);
		}
		#endregion

		#region 出库
		public virtual ActionResult OutPopup(int eId)
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageEquipments))
				return AccessDeniedView();

			var model = new EquipmentStockLogModel
			{
				RelatedType = (int)StockRelatedTypeEnum.出库
			};
			if (eId > 0)
			{
				var equipment = _xJEquipmentService.GetXJEquipmentById(eId);
				if (null != equipment)
				{
					model.RelatedTypeName = Enum.GetName(typeof(StockRelatedTypeEnum), model.RelatedType);
					model.EquipmentId = equipment.Id;
					model.Name = equipment.Name;
					model.Manufacturer = equipment.Manufacturer;
					model.Specification = equipment.Specification;
					model.Price = equipment.Price;
					model.Amount = 0;
					model.Comment = null;
					model.CreatedOn = DateTime.Now;
				}
			}
			return View(model);
		}

		[HttpPost]
		[FormValueRequired("save")]
		public virtual ActionResult OutPopup(string btnId, EquipmentStockLogModel model)
		{
			if (!_permissionService.Authorize(StandardPermissionProvider.ManageEquipments))
				return AccessDeniedView();

			if (ModelState.IsValid)
			{
				var equipment = _xJEquipmentService.GetXJEquipmentById(model.EquipmentId);
				if (null == equipment)
				{
					ErrorNotification("设施设备无效");
					ViewBag.RefreshPage = false;
					ViewBag.btnId = btnId;
					return View(model);
				}
				var stockLog = new XJEquipmentStockLog
				{
					RelatedType = (int)StockRelatedTypeEnum.出库,
					EquipmentId = model.EquipmentId,
					Price = model.Price,
					Name = model.Name,
					Manufacturer = model.Manufacturer,
					Specification = model.Specification,
					Amount = model.Amount,
					Comment = model.Comment,
					CustomerId = _workContext.CurrentCustomer.Id,
					CreatedOn = DateTime.Now,
				};
				_xJEquipmentStockLogService.InsertXJEquipmentStockLog(stockLog);

				equipment.Stock -= model.Amount;
				_xJEquipmentService.UpdateXJEquipment(equipment);

				//activity log
				_customerActivityService.InsertActivity("AddNewEquipmentStockLog", "新增出库：{0}", stockLog.Id);
			}

			ViewBag.RefreshPage = true;
			ViewBag.btnId = btnId;
			return View(model);
		}
		#endregion

	}
}